<script>
import '.env.js'
import {mapState, mapMutations} from 'vuex'
import {addXHXFtoken, postDayVisits, postDayVisitsUrl, hzzghLogin,autoLoginx} from '@/common/request.js'
import {dateFormat} from '@/common/functions.js'

let pageJson = require('@/static/pages.json')
export default {
  mounted () {
// #ifdef  H5
    if (window.AlipayJSBridge) {
      this.callback && this.callback()
    } else {
      document.addEventListener('AlipayJSBridgeReady', this.callback, false)
    }

    this.AppMsgFun()
    this.AutoLoginParam(window.location.href)
	// #endif
  },
  methods: {
    ...mapMutations(['getUserInfo', 'saveUserInfo']),
    //进行蓝领一家免登操作吧
    AutoLoginParam(urlParams){
      const url = urlParams
      if(url?.includes('autoLoginParam')){
        const that = this
        const urlSplit = url.split('autoLoginParam=')[1]
        const decodeURIString =decodeURIComponent(urlSplit)
        const jsonParse = JSON.parse(decodeURIString)
        const data ={
          ...jsonParse
        }
        autoLoginx(data).then(res=>{
           that.setStorageSyncToken(res,that)
        })
      }
    },
    //保存用户的信息
    setStorageSyncToken(res,that){
      const {success,result:{token,userInfo}}=res
      if (success) {
        //写入缓存
        uni.setStorageSync('token', token)
        that.saveUserInfo(userInfo, token)
        that.getUserInfo()
      }
    },
    callback () {
      let that = this
      AlipayJSBridge.call('getLoginedUserToken', {}, function (result) {
        //alert(JSON.stringify(result));
        if (result && result.token) {
          addXHXFtoken(result.token).then(res => {
            that.setStorageSyncToken(res,that)
          })
        }
      })
    },
    AppMsgFun (obj) {
      window.arouseNativeMsg = function (obj) {
        //alert('----')
        const temp = typeof obj == 'String' ? JSON.parse(obj) : obj
        if (!!temp.userId == false) {
          alert('获取用户信息失败')
          return
        }
        // {"timeStamp":"1622529697000",
        // "sign":"54784E8368A5DF786AD659AEA77506A55339D6C6",
        // "bind_mobile":"137****6172",
        // "name":"",
        // "userId":"AmIOcsvkmUk="}
        const {bind_mobile, name, sign, timeStamp, userId} = temp

        //post server
        let data = {
          'h5sign': sign,
          'mobile': bind_mobile,
          'name': name,
          'timestamp': timeStamp,
          'userId': userId
        }
        //alert(JSON.stringify(data))
        hzzghLogin(data).then(res => {
          if (res.success) {
            //写入缓存
            uni.setStorageSync('token', res.result.token)
            that.saveUserInfo(res.result.userInfo, res.result.token)
            that.getUserInfo()
          }
        })
      }
      const browser = {
        versions: function () {
          const u = navigator.userAgent
          return {
            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
            android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
          }
        }()
      }
      console.log('browser.versions.ios', browser.versions.ios)
      console.log('browser.versions.android', browser.versions.android)
      if (browser.versions.ios) {//判读iOS或安卓
        try {
          window.webkit.messageHandlers.requestApp.postMessage({
            'type': 'encryption_user_msg_type',
            'callBack': {
              'callBackName': 'arouseNativeMsg',
              'user_id': '加密用户ID',
              'bind_mobile': '绑定的手机号脱敏',
              'name': '用户名脱敏',
              'timeStamp': '时间戳',
              'sign': '签名'
            }
          })
          return 'ios'
        } catch (err) {
          // on catch
        }
      } else if (browser.versions.android) {
        try {
          window.forJs.requestApp(`{
				      "type":"encryption_user_msg_type",
				      "callBack": {
				        "callBackName": "arouseNativeMsg",
				        "callBackParms": {
				            "user_id": "",
				            "name": "",
				            "bind_mobile": "",
				            "timeStamp": "",
				            "sign": ""
				          }
				        }
				    }`)
          return 'android'
        } catch (err) {
          // on catch
        }
      } else {
        console.log('不是安卓或者IOS')
      }
    },

  },
// #ifdef  H5
  watch: {
    $route (e) {
      let url = ''
      let title = ''
      if (e.meta && e.meta.pagePath) {
        url = e.meta.pagePath
      }
      if (pageJson && pageJson.pages) {
        let arr = pageJson.pages.filter(x => x.path == url)
        if (arr && arr.length) {
          let page = arr[0]
          title = page.style.navigationBarTitleText
        }
      }
	  if(url!='pages/webview/webview'){
		  postDayVisitsUrl(url, title)
	  }


      let tag = uni.getStorageSync('tag') || '1'
      if (tag) {
        let strToday = uni.getStorageSync('today')
        let strNow = dateFormat(new Date(), 'yyyy-MM-dd')
        if (strToday == null || strToday != strNow) {
          uni.setStorageSync('today', strNow)
          postDayVisits(tag, 0)
        } else {
          postDayVisits(tag, 1)
        }

      }
    }
  },
  globalData: process.uniEnv,
  onLaunch: function () {
    this.getUserInfo()
    console.log('App Launch')
  },
  // #endif
  onShow: function () {
    console.log('App Show')
  },
  onHide: function () {
    console.log('App Hide')
  }
}
</script>
<style lang="scss">
@import "colorui/main.css";
@import "colorui/icon.css";
/*每个页面公共css */
.container {
  position: relative;
}

.ellipse {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

p img {
  width: 100%;
}

.tab-botton {
  padding-bottom:140rpx !important;
}

.loadmore-not {
  padding-top: 15%
}

.more-data {
  position: absolute;
  right: 60rpx;
  width: 91rpx;
  height: 19rpx;
  font-size: 26rpx;
  font-family: PingFang;
  font-weight: 500;
  color: #006be8;
  line-height: 44rpx;
  white-space: nowrap;
}

.tab-view {
  width: 28%;
  background: #d6d6d6;
  border-radius: 20rpx;
  white-space: nowrap;
  display: flex;
  justify-content: space-between;

  .t0 {
    width: 33.33%;
    font-family: PingFang;
    font-weight: 500;
    color: #222222;
    text-align: center;
    font-size: 28rpx;
    height: 41rpx;
    line-height: 41rpx;

    &:last-child {
    }
  }

  .select-text {
    background: #e9e9e9;
    border-radius: 20rpx;
  }

  view {
    display: inline-block;
    font-size: 20rpx;
  }
}
.groupClass{
  margin-top: 20rpx;
  background-color: #ffffff;
  padding: 20rpx 40rpx 40rpx 40rpx;
  border-radius: 20rpx;
}
</style>
<style lang="scss">

/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
@import "uview-ui/index.scss";
</style>
